<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/06/18 21:38:31 $)
// $RCSfile: functions_workschedule.php,v $ : $Revision: 1.3 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Builds up the select options for all hours.. generated at an interval of 15 minutes
*/
function buildHourSelectOptions($isclosed = false, $fieldname = "")
{
	global $_SWIFT;

	$_options = null;
	$minutes = 0;
	if (!empty($fieldname))
	{
		$nosel = true;
	} else {
		$nosel = false;
	}

	for ($ii=0; $ii<96; $ii++)
	{
		unset($seltext);
		$_hour = floor($minutes/60);
		$_minute = floor($minutes-($_hour*60));
		$_ushour = iif($_hour>=12, iif(($_hour-12)==0, "12", ($_hour-12)), iif($_hour==0, "12", $_hour));
		$_ampm = iif($_hour>=12, $_SWIFT["language"]["pm"], $_SWIFT["language"]["am"]);

		$euhr = sprintf($_SWIFT["language"]["hourrendereu"], iif($_hour=="0", "00", $_hour), iif($_minute=="0", "00", $_minute));
		$ushr = sprintf($_SWIFT["language"]["hourrenderus"], iif($_ushour=="0", "00", $_ushour), iif($_minute=="0", "00", $_minute), $_ampm);
		$storehr = sprintf("%02d", $_hour) .":". sprintf("%02d", $_minute);

		if (($isclosed == false && $ii == 0 && empty($_POST[$fieldname])) || ($isclosed == true && $ii == 95 && empty($_POST[$fieldname])))
		{
			$seltext = iif($nosel==false, " selected");
		}

		if ($_POST[$fieldname] == $storehr) {
			$seltext = " selected";
		}

		$_options .= '<option value="'. $storehr .'"'. $seltext .'>'. htmlspecialchars($ushr) .'</option>'.SWIFT_CRLF;
		$minutes = $minutes+15;
	}

	return $_options;
}

/**
* Update the given Staff Schedule
*/
function updateStaffSchedule($staffid, $_args)
{
	global $dbCore, $_SWIFT;

	if (!is_array($_args) || !count($_args))
	{
		return false;
	}

	$dbCore->query("UPDATE `". TABLE_PREFIX ."staffschedules` SET `sunday_enabled` = '". intval($_args["sunday_enabled"]) ."', `sunday_open` = '". $dbCore->escape($_args["sunday_open"]) ."', `sunday_close` = '". $dbCore->escape($_args["sunday_close"]) ."', `monday_enabled` = '". intval($_args["monday_enabled"]) ."', `monday_open` = '". $dbCore->escape($_args["monday_open"]) ."', `monday_close` = '". $dbCore->escape($_args["monday_close"]) ."', `tuesday_enabled` = '". intval($_args["tuesday_enabled"]) ."', `tuesday_open` = '". $dbCore->escape($_args["tuesday_open"]) ."', `tuesday_close` = '". $dbCore->escape($_args["tuesday_close"]) ."', `wednesday_enabled` = '". intval($_args["wednesday_enabled"]) ."', `wednesday_open` = '". $dbCore->escape($_args["wednesday_open"]) ."', `wednesday_close` = '". $dbCore->escape($_args["wednesday_close"]) ."', `thursday_enabled` = '". intval($_args["thursday_enabled"]) ."', `thursday_open` = '". $dbCore->escape($_args["thursday_open"]) ."', `thursday_close` = '". $dbCore->escape($_args["thursday_close"]) ."', `friday_enabled` = '". intval($_args["friday_enabled"]) ."', `friday_open` = '". $dbCore->escape($_args["friday_open"]) ."', `friday_close` = '". $dbCore->escape($_args["friday_close"]) ."', `saturday_enabled` = '". intval($_args["saturday_enabled"]) ."', `saturday_open` = '". $dbCore->escape($_args["saturday_open"]) ."', `saturday_close` = '". $dbCore->escape($_args["saturday_close"]) ."' WHERE `staffid` = '". intval($staffid) ."';");

	return true;
}

/**
* Checks the given hour range. Hours should be in 00:00 (EU) Format.. if the second hr is less than the first one, it errors out
*/
function checkHourRange($firsthr, $secondhr)
{
	// We always expect to have a 24 hr format
	$hrtime1 = explode(":", $firsthr);
	$hrtime2 = explode(":", $secondhr);

	$hrtimeresult1 = mktime($hrtime1[0], $hrtime1[1], 0, date("m"), date("d"), date("Y"));
	$hrtimeresult2 = mktime($hrtime2[0], $hrtime2[1], 0, date("m"), date("d"), date("Y"));

	// secondhr will always have to be greater than the first hour
	if ($hrtimeresult2 <= $hrtimeresult1)
	{
		return "-1";
	} else if ($hrtimeresult2 == $hrtimeresult1) {
		return "0";
	} else {
		$timeresult = $hrtimeresult2-$hrtimeresult1;
		return $timeresult;
	}
}

/**
* Returns the representation of hour according to date setting
*/
function returnParsedHour($houreu)
{
	global $_SWIFT;

	$_hourus = $_houreu = "";

	$_hourarray = explode(":", $houreu);
	
	$_hour = $_hourarray[0];
	$_minute = $_hourarray[1];

	if ($_hour > 12)
	{
		$_ushour = iif(($_hour-12)==0, "12", ($_hour-12));
	} else {
		$_ushour = iif($_hour==0, "12", $_hour);
	}

	$_ampm = iif($_hour>=12, $_SWIFT["language"]["pm"], $_SWIFT["language"]["am"]);

	$euhr = sprintf($_SWIFT["language"]["hourrendereu"], iif($_hour=="0", "00", $_hour), iif($_minute=="0", "00", $_minute));
	$ushr = sprintf($_SWIFT["language"]["hourrenderus"], iif($_ushour=="0", "00", $_ushour), iif($_minute=="0", "00", $_minute), $_ampm);

	return $ushr;
}
?>